package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.CategoryBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "分类品牌")
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {

    @Autowired
    private CategoryBrandService categoryBrandService;


    @Operation(summary = "分页带条件的查询")
    @GetMapping("/list")
    public TableDataInfo getPageList(CategoryBrand categoryBrand){
        startPage();
        List<CategoryBrand> categoryBrandList=categoryBrandService.getPageList(categoryBrand);
        return getDataTable(categoryBrandList);
    }

    @Operation(summary = "新增分类品牌")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated CategoryBrand categoryBrand){
        //设置添加品牌的用户
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        boolean save= categoryBrandService.saveCategoryBrand(categoryBrand);
         return toAjax(save);
    }

    @Operation(summary = "根据Id查询品牌信息")
    @GetMapping ("/{id}")
    public AjaxResult getCategoryBrandById(@PathVariable  Long id){
        CategoryBrand categoryBrand = categoryBrandService.getCategoryBrandById(id);
        return AjaxResult.success(categoryBrand);
    }

    @Operation(summary = "根据Id修改品牌信息")
    @PutMapping
    public AjaxResult updateCategoryBrandById(@RequestBody CategoryBrand categoryBrand){
        //设置更新者
        categoryBrand.setUpdateBy(SecurityUtils.getUsername());
        int flag= categoryBrandService.updateCategoryBrandById(categoryBrand);
        return toAjax(flag);
    }


    @Operation(summary = "删除分类品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids){
        return toAjax(categoryBrandService.removeBatchByIds(ids));
    }

    @Operation(summary = "根据分类id查询所有品牌")
    @GetMapping("/brandList/{categoryId}")
    public AjaxResult getBrandListBycategoryId(@PathVariable Long categoryId ){
       List<Brand>  brandList= categoryBrandService.getBrandListBycategoryId(categoryId);
       return success(brandList);
    }



}
